<?php
include_once('./inc/config.php');
if(is_logged_in()) redirect('dashboard.php');	

$mode = $_POST['mode'];
$actions = array();

switch($mode)
{
	case 'check':
		$actions = check_user();
	break;	

	default:
	//do nothing
 	break;	
}

/** 
 * Check User
 *
 * @descp Validate User in the DB
 * @access public
 * @param  NA	 
 * @return void
 */
function check_user()
{
	global $dbobj, $proj_config;
	
	$ses_arr = array('user_id','user_email');
	if(!session_register($ses_arr))
		session_register($ses_arr);

	$set = array();
	
	$errors = '';
	
	$user_name 	= $_POST['user_name'];
	$user_pwd 	= $_POST['user_pwd'];
	
	if(empty($user_name) || empty($user_pwd))
	{
		$set['errors'] = '<li>Please enter a valid username/password</li>';
	}
	else
	{
		$query = "SELECT * FROM ". TBL_USERS ."
					WHERE user_email = '".$dbobj->MySQLDB_Sanitize($user_name)."'	
					AND user_pwd  = '".md5($user_pwd)."'";
			
		// Perform Query
		$result = $dbobj->MySQLDB_GetData($query);
		// Get num rows from the result set	
		$no_rows = $dbobj->MySQLDB_Numrows();
		
		if($no_rows > 0)
		{
			// Use result			
			$_SESSION['user_id'] 	= $result['user_id'];
			$_SESSION['user_email'] = $result['user_email'];			
			redirect('dashboard.php');
		}
		else
		{
			$set['errors'] = '<li>Please enter a valid username/password</li>';	
		}
		$dbobj->MySQLDB_Free($result);						
	}
	return $set;  
}


/*************************************************************/
//Do not chnage this part
include(BASEPATH . 'views/header.php');//include header file
include(BASEPATH . 'views/login.php');//include body part
include(BASEPATH . 'views/footer.php');//include footer file
/************************************************************/
?>